from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    _stack = []
    _stack_flag = False
    divs = []
    _div_names = []
    _div_name = ''
    _start = 0
    _end = 0

    def SetSectionName(self, div_names):
        self._div_names = div_names

    def handle_starttag(self, tag, attrs):
        if self._stack_flag == False:
            if len(self._div_names) != len(self.divs) and tag == 'div' and len(attrs) != 0:
                for (name, value) in attrs:
                    if name == 'id':
                        for div_name in self._div_names:
                            if value == div_name:
                                self._stack_flag = True
                                self._stack.append(tag)
                                self._div_name = div_name
                                (self._start, offset) = self.getpos()
        else:
            if tag == 'div':
                self._stack.append(tag)

    def handle_endtag(self, tag):
        if self._stack_flag == True:
            if tag == 'div':
                self._stack.pop()
                if len(self._stack) == 0:
                    self._stack_flag = False
                    (self._end, offset) = self.getpos()
                    self.divs.append((self._div_name, self._start - 1, self._end))
